//
//  MarketViewController.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/2.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "MarketViewController.h"
#import "MenuTool.h"
#import "HMSegmentedControl.h"
#import "JMColumnMenu.h"
#import "HangQingCell.h"
#import "HQShiZhiCell.h"
#import "ValueModel.h"
#import "MenuToolMarket.h"
#import "PageContollerCell.h"
#import "MarketBaseController.h"

static NSString *reuseID = @"itemCell1";

@interface MarketViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,JMColumnMenuDelegate>
{
    UITableView *_tableView1;

}

@property(nonatomic, copy)NSArray *showHQMenua;
@property (nonatomic ,retain) HMSegmentedControl *header;
/** menuView */
@property (nonatomic, strong) JMColumnMenu *menu;
@property(nonatomic, strong)UIButton *menuButton;
@property (nonatomic ,retain)UICollectionView * collectionView;


@property(nonatomic, strong)NSMutableArray *aticleArray;
@property(nonatomic, assign)NSInteger pageSize;
@property(nonatomic, assign)NSInteger pageIndex;
@end

@implementation MarketViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.aticleArray = [NSMutableArray array];
    self.showHQMenua=[MenuToolMarket userSelectedMenu];
    [self createNav];
//    [self setupUI];
    [self createCollectionView];
    // Do any additional setup after loading the view.
    
    self.pageSize = 20;
    self.pageIndex = 1;

    
    /** MJRefresh的block也需要用若引用 */
    WEAKSELF
    _tableView1.mj_header = [XMRefreshHeader xm_headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        [self.aticleArray removeAllObjects];
        [weakSelf getMarketValueInfo];
    }];
    //默认block方法：设置上拉加载更多
    _tableView1.mj_footer = [XMRefreshFooter xm_footerWithRefreshingBlock:^{
        self.pageIndex += 1;
        [weakSelf getMarketValueInfo];
    }];
    [_tableView1.mj_header beginRefreshing];

}
-(void)getMarketValueInfo{
    NSDictionary * dict = @{@"PageInfo":@{@"CurrentPage":@(self.pageIndex),
                                          @"PageSize":@(self.pageSize)
                                          }
                            };
    [FBNetworkTool get:@"quotationapi/com.getMarketValueInfo" parameters:dict showHUD:NO responseCache:^(XMHttpResponseModel *responseModel) {
        NSArray * array = [ValueModel mj_objectArrayWithKeyValuesArray:responseModel.data];
        [self.aticleArray addObjectsFromArray:array];
        [_tableView1 reloadData];
        
    } success:^(XMHttpResponseModel *responseModel) {
        [_tableView1.mj_header endRefreshing];
        [_tableView1.mj_footer endRefreshing];
        
        NSArray * array = [ValueModel mj_objectArrayWithKeyValuesArray:responseModel.data];
        [self.aticleArray addObjectsFromArray:array];
        if (array.count == 0) {
            [_tableView1.mj_footer endRefreshingWithNoMoreData];
        }
        
        [_tableView1 reloadData];
        
    } failure:^(NSError *error) {
        [_tableView1.mj_header endRefreshing];
        [_tableView1.mj_footer endRefreshing];
        
    }];
    
}
-(void)createNav{
    
    self.header = [[HMSegmentedControl alloc] init];
    self.header.frame = CGRectMake(0, 0, self.view.bounds.size.width - 44, 44);
    self.header.backgroundColor = [UIColor whiteColor];
    self.header.segmentEdgeInset = UIEdgeInsetsMake(0, 12, 0, 12);
    
    //    self.header.selectedTitleTextAttributes = @{NSForegroundColorAttributeName : [UIColor xm_MainColor],NSFontAttributeName:[UIFont xm_medium:16.f]};
    self.header.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor xm_colorFromRGB:0xB1BCCB],NSFontAttributeName:[UIFont xm_font:16.f]};
    self.header.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;//设定线条的位置
    self.header.selectionIndicatorColor = [UIColor xm_MainColor];
    self.header.selectionIndicatorHeight = 3.f;
    self.header.segmentWidthStyle=HMSegmentedControlSegmentWidthStyleFixed;
    [self.header addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.header];
    [self setSegmentedTitleText];
    
    self.menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.menuButton.frame = CGRectMake(self.view.width - 44, 0, 44, 44);
    [self.menuButton setImage:[UIImage imageNamed:@"home_filter"] forState:UIControlStateNormal];
    [self.menuButton addTarget:self action:@selector(menuButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.menuButton];
    
}
-(void)setupUI{
    UITableView *tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, SCREEN_HEIGHT-kNav_Height - kTabBarHeight - 44) style:UITableViewStyleGrouped];
    _tableView1 = tableView1;
    tableView1.tag = 1;
    tableView1.dataSource = self;
    tableView1.delegate = self;
//    tableView1.emptyDataSetSource = self;
//    tableView1.emptyDataSetDelegate = self;
    tableView1.tableFooterView = [UIView new];
    tableView1.backgroundColor = [UIColor clearColor];
    tableView1.separatorColor = kLightGrayColor;
    [self.view addSubview:tableView1];
    
}
-(void)setSegmentedTitleText{
    NSMutableArray * array = [NSMutableArray array];
    for (AticleMenu * menu in self.showHQMenua) {
        [array addObject:menu.Text];
    }
    self.header.sectionTitles = array;
}
- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl{
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:segmentedControl.selectedSegmentIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}

-(void)createCollectionView{
    UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT-kNav_Height - kTabBarHeight - 44);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView =[[UICollectionView alloc]initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, SCREEN_HEIGHT-kNav_Height - kTabBarHeight - 44) collectionViewLayout:layout];
    self.collectionView.delegate =self;
    self.collectionView.dataSource = self;
    self.collectionView.bounces = YES;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.scrollEnabled=NO;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[PageContollerCell class] forCellWithReuseIdentifier:reuseID];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.showHQMenua.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PageContollerCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
    AticleMenu * menu = self.showHQMenua[indexPath.row];
    if (menu.menuId == 206) {//视屏
        MarketBaseController * baseVC = [[MarketBaseController alloc] init];
        [cell.contentView addSubview:baseVC.view];
        baseVC.menu = self.showHQMenua[indexPath.row];
        [self addChildViewController:baseVC];
        return cell;
        
    }else{
        MarketBaseController * baseVC = [[MarketBaseController alloc] init];
        baseVC.menu = self.showHQMenua[indexPath.row];

        [cell.contentView addSubview:baseVC.view];
        [self addChildViewController:baseVC];
        return cell;
    }
    
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    CGFloat offsetx =scrollView.contentOffset.x;
    NSInteger index = offsetx/SCREEN_WIDTH;
    [self.header setSelectedSegmentIndex:index animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
